﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using SpecificUserControl;


namespace InOutSystem
{
    public partial class MainForm : Form
    {
        private AppDomain domainSpecificUserControl;
        private Assembly assSpecificUserControl;

        public MainForm()
        {
            InitializeComponent();

            domainSpecificUserControl = AppDomain.CurrentDomain;
            assSpecificUserControl = domainSpecificUserControl.Load("SpecificUserControl");
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Load_NaviBar();
        }

        private void Load_NaviBar()
        {
            Assembly assSpecificUserControl = domainSpecificUserControl.Load("SpecificUserControl");
            Stream streamConfig = assSpecificUserControl.GetManifestResourceStream("SpecificUserControl.ControlConfig.xml");

            XmlReaderSettings xrs = new XmlReaderSettings();
            xrs.DtdProcessing = DtdProcessing.Parse;

            XmlReader xr_Config = XmlReader.Create(streamConfig, xrs);
            XElement xe_Config = XElement.Load(xr_Config);

            //按Index顺序查询所有Item项
            var item_query = from XElement xe_Item in xe_Config.Elements("item")
                             let index = Convert.ToInt32(xe_Item.Element("display_index").Value)
                             orderby index
                             select xe_Item;
            foreach (XElement xe_Item in item_query)
            {
                ControlConfig config_item = new ControlConfig();
                config_item.DisplayIndex = Convert.ToInt32(xe_Item.Element("display_index").Value);
                config_item.AppName = xe_Item.Element("app_name").Value;
                config_item.NaviName = xe_Item.Element("navi_name").Value;
                config_item.IconDec = xe_Item.Element("icon_dec").Value;
                config_item.ModuleName = xe_Item.Element("module_name").Value;

                AddOneModule(config_item);
            }
        }

        private void AddOneModule(ControlConfig config)
        {
            Button btn_OneModule = new Button();
            btn_OneModule.Name = string.Format("btn_{0}", config.AppName);
            btn_OneModule.Width = flow_MainUserControl.Width - flow_MainUserControl.Margin.Left - flow_MainUserControl.Margin.Right;
            btn_OneModule.Text = config.NaviName;
            btn_OneModule.Tag = config;
            btn_OneModule.Click += new EventHandler(btn_OneModule_Click);

            flow_MainUserControl.Controls.Add(btn_OneModule);
        }

        private void ClearControls()
        {
            while (splitc_Mainform.Panel2.Controls.Count > 0)
            {
                splitc_Mainform.Panel2.Controls.Clear();
            }
        }

        void btn_OneModule_Click(object sender, EventArgs e)
        {
            Button btn_OneModule = sender as Button;
            ControlConfig config = btn_OneModule.Tag as ControlConfig;

            string strFormName = config.ModuleName;
            Form form_UserControl = (Form)assSpecificUserControl.CreateInstance(strFormName);
            if (form_UserControl != null)
            {
                splitc_Mainform.Panel2.Controls.Clear();
                form_UserControl.TopLevel = false;

                splitc_Mainform.Panel2.Controls.Add(form_UserControl);
                form_UserControl.Show();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Moonbug m = new Moonbug();
            m.Show();
        }


    }
}
